package com.naiterui.ehp.bs.cms.modules.agent.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.advice.IgnoreResponseEntityWrap;
import com.naiterui.ehp.bp.support.utils.FileUtil;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.agent.service.IBusinessReportService;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaStatisticsDetailVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaStatisticsReportVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.AreaStatisticsVO;
import com.naiterui.ehp.bs.cms.modules.agent.vo.BusinessStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 营业报表RecomServiceImpl
 * 
 * @author yangfuya
 * @since 2022/2/14
 */
@Slf4j
@Api(tags = {SwaggerTag.CMS + SwaggerTag.AGENT_MANGEN})
@AllArgsConstructor
@RestController
@RequestMapping("/business/report")
@PreAuthorize("@p.check('area')")
public class BusinessReportController extends AbstractController {
    private final IBusinessReportService businessReportService;
    

    /**
     * 全平台数据统计
     */
    @SysLog("全平台数据统计")
    @ApiOperation("全平台数据统计")
    @GetMapping("/statistics")
    // @PreAuthorize("@p.check('area:business:report:statistics')")
    public BusinessStatisticsVO getStatistics(Long areaId) {
        return businessReportService.getStatistics(areaId);
    }

    @SysLog("大区列表")
    @GetMapping("/area/list")
    @ApiOperation("大区列表")
    // @PreAuthorize("@p.check('area:business:report:list')")
    public PageVO<AreaStatisticsVO> areaList(PageParamsVO pageParamsVO) {
        return this.businessReportService.areaList(pageParamsVO);
    }

    @SysLog("大区详情")
    @GetMapping("/area/detail")
    @ApiOperation("大区详情")
    @ApiImplicitParam(name = "areaId", value = "大区Id", dataType = "long", paramType = "query", required = true)
    public AreaStatisticsDetailVO detail(@RequestParam Long areaId) {
        return this.businessReportService.detail(areaId);
    }

    @SysLog("大区/省区报表列表")
    @GetMapping("/list")
    @ApiOperation("大区/省区报表列表")
    // @PreAuthorize("@p.check('area:report:list')")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "areaId", value = "大区Id", dataType = "long", paramType = "query", required = true),
        @ApiImplicitParam(name = "type", value = "类型：1.大区 2.省区", dataType = "int", paramType = "query",
            required = true),
        @ApiImplicitParam(name = "reportType", value = "报告类型：1.年 2.月 3.日", dataType = "int", paramType = "query",
            required = true),
        @ApiImplicitParam(name = "reportDate", value = "年月日 yyyy-MM-dd格式", dataType = "string", paramType = "query")})
    public PageVO<AreaStatisticsReportVO> list(PageParamsVO pageParamsVO, @RequestParam Long areaId,
        @RequestParam Integer type, @RequestParam Integer reportType, String reportDate) {
        return this.businessReportService.list(pageParamsVO, areaId, type, reportType, reportDate);
    }

    @GetMapping("/export")
    @ApiOperation("大区/省区导出")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "areaIds", value = "大区Id，type=2时传递省区id，多个id逗号分割", dataType = "string",
            paramType = "query", required = true),
        @ApiImplicitParam(name = "type", value = "类型：1.大区 2.省区", dataType = "int", paramType = "query",
            required = true),
        @ApiImplicitParam(name = "reportType", value = "报告类型：1.年 2.月 3.日", dataType = "int", paramType = "query",
            required = true),
        @ApiImplicitParam(name = "reportDates", value = "选择报告日期，多个日期以逗号分割", dataType = "string", paramType = "query")})
    @IgnoreResponseEntityWrap
    // @PreAuthorize("@p.check('area:report:export')")
    public void export(String areaIds, Integer type, Integer reportType, String reportDates,
        HttpServletResponse response) throws IOException {
        log.info("大区/省区导出, areaIds: {}, type: {}, reportType: {}, reportDates: {}", areaIds, type, reportType,
            reportDates);
        byte[] body = this.businessReportService.export(areaIds, type, reportType, reportDates);
        String fileName = System.getProperty("java.io.tmpdir") + File.separator + System.currentTimeMillis() + ".xls";
        FileUtil.downloadFile(this.request, response, FileUtil.writeBytes(body, fileName), true);
    }
}
